package app.service;

import app.bean.Column;
import app.bean.Table;
import app.util.CommonUtil;
import app.util.FtlUtil;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class AppCodeService {

    private Logger logger = LoggerFactory.getLogger(AppCodeService.class);

    static final String FTL_ENTITY = "entity.java.ftl";

    static final String FTL_DAO = "dao.java.ftl";

    static final String FTL_MAPPER = "mapper.xml.ftl";

    static final String FTL_SERVICE = "service.java.ftl";

    static final String FTL_CONTROLLER = "controller.java.ftl";

    @Autowired
    MySqlService mySqlService;

    public void createCode(String dbname){
        String beanPackage = "gz.beans";
        String daoPackage = "gz.dao";
        String servicePackage = "gz.service";
        String mapperPackage = "gz.mapper";
        String controllerPackage = "gz.controller";
        entityCode(dbname,beanPackage);
        daoCode(dbname,daoPackage,beanPackage);
        mapperCode(dbname,mapperPackage,beanPackage);
        serviceCode(dbname,servicePackage,beanPackage,daoPackage);
        controllerCode(dbname,controllerPackage,beanPackage,servicePackage);
        mapperCode(dbname,mapperPackage,daoPackage);
    }

    public void entityCode(String dbname,String basePackage){
        List<Table> tab2 = mySqlService.getTables(dbname);
        logger.info(Arrays.toString(tab2.toArray())+",共计"+tab2.size()+"张表");
        for(Table table:tab2){
            table = mySqlService.getColumns(dbname,table);

            Map<String,Object> data = new HashMap<>();
            data.put("basePackage",basePackage);
            data.put("className",table.getClassName());
            data.put("cname",table.getCname());
            data.put("columns",table.getColumns());
            String outPath = CommonUtil.appPath()+ "/src/main/java/"+basePackage.replaceAll("\\.","/")+"/"+table.getClassName()+".java";
            logger.info("输出路径{}",outPath);
            FtlUtil.createOut(data,FTL_ENTITY,outPath);
        }
    }

    public void daoCode(String dbname,String basePackage,String beanPackage){
        List<Table> tab2 = mySqlService.getTables(dbname);
        logger.info(Arrays.toString(tab2.toArray())+",共计"+tab2.size()+"张表");
        for(Table table:tab2){
            table = mySqlService.getColumns(dbname,table);

            Map<String,Object> data = new HashMap<>();
            data.put("basePackage",basePackage);
            data.put("beanPackage",beanPackage);
            data.put("className",table.getClassName());
            data.put("humpName",table.getHumpName());
            data.put("cname",table.getCname());
            data.put("columns",table.getColumns());
            String outPath = CommonUtil.appPath()+ "/src/main/java/"+basePackage.replaceAll("\\.","/")+"/"+table.getClassName()+"Dao.java";
            logger.info("输出路径{}",outPath);
            FtlUtil.createOut(data,FTL_DAO,outPath);
        }
    }

    public void mapperCode(String dbname,String basePackage,String daoPackage){
        List<Table> tab2 = mySqlService.getTables(dbname);
        logger.info(Arrays.toString(tab2.toArray())+",共计"+tab2.size()+"张表");
        for(Table table:tab2){
            table = mySqlService.getColumns(dbname,table);

            Map<String,Object> data = new HashMap<>();
            data.put("basePackage",basePackage);
            data.put("daoPackage",daoPackage);
            data.put("className",table.getClassName());
            data.put("humpName",table.getHumpName());
            data.put("name",table.getName());
            data.put("columns",table.getColumns());
            String outPath = CommonUtil.appPath()+ "/src/main/resources/mapper/"+basePackage.replaceAll("\\.","/")+"/"+table.getClassName()+"Mapper.xml";
            logger.info("输出路径{}",outPath);
            FtlUtil.createOut(data,FTL_MAPPER,outPath);
        }
    }

    public void serviceCode(String dbname,String basePackage,String beanPackage,String daoPackage){
        List<Table> tab2 = mySqlService.getTables(dbname);
        logger.info(Arrays.toString(tab2.toArray())+",共计"+tab2.size()+"张表");
        for(Table table:tab2){
            table = mySqlService.getColumns(dbname,table);

            Map<String,Object> data = new HashMap<>();
            data.put("basePackage",basePackage);
            data.put("beanPackage",beanPackage);
            data.put("daoPackage",daoPackage);
            data.put("className",table.getClassName());
            data.put("humpName",table.getHumpName());
            data.put("cname",table.getCname());
            data.put("columns",table.getColumns());
            String outPath = CommonUtil.appPath()+ "/src/main/java/"+basePackage.replaceAll("\\.","/")+"/"+table.getClassName()+"Service.java";
            logger.info("输出路径{}",outPath);
            FtlUtil.createOut(data,FTL_SERVICE,outPath);
        }
    }

    public void controllerCode(String dbname,String basePackage,String beanPackage,String servicePackage){
        List<Table> tab2 = mySqlService.getTables(dbname);
        logger.info(Arrays.toString(tab2.toArray())+",共计"+tab2.size()+"张表");
        for(Table table:tab2){
            table = mySqlService.getColumns(dbname,table);

            Map<String,Object> data = new HashMap<>();
            data.put("basePackage",basePackage);
            data.put("beanPackage",beanPackage);
            data.put("servicePackage",servicePackage);
            data.put("className",table.getClassName());
            data.put("humpName",table.getHumpName());
            data.put("cname",table.getCname());
            data.put("columns",table.getColumns());
            String outPath = CommonUtil.appPath()+ "/src/main/java/"+basePackage.replaceAll("\\.","/")+"/"+table.getClassName()+"Controller.java";
            logger.info("输出路径{}",outPath);
            FtlUtil.createOut(data,FTL_CONTROLLER,outPath);
        }
    }

}
